package zhuhai.tedu.parttimess.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.DigestUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import zhuhai.tedu.parttimess.domain.Result;
import zhuhai.tedu.parttimess.domain.Student;
import zhuhai.tedu.parttimess.domain.User;
import zhuhai.tedu.parttimess.domain.vo.UserVo;
import zhuhai.tedu.parttimess.service.UserService;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@RestController
@RequestMapping(value = "/user/")
@Api(value = "用户")
public class UserController  extends BaseController{

    private static final Logger LOG = LoggerFactory.getLogger("UserController");

    @Autowired
    private HttpSession session;
    @Autowired
    private UserService userService;

    @ApiOperation(value = "分页查询")
    @RequestMapping(method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页面大小", name = "size", paramType = "query"),
            @ApiImplicitParam(value = "当前页面", name = "page", paramType = "query")
    })
    private Page<UserVo> findByPage(final UserVo userVo, Pageable pageable) {
        Page<User> page= userService.findByPage(userVo.toUser(), pageable);
        return page.map(m->new UserVo(m)) ;
    }

    @ApiOperation(value = "添加用户")
    @RequestMapping(method = RequestMethod.POST)
    private Result add(@Valid @RequestBody UserVo userVo, final BindingResult result) {
        try {
            if (!result.hasErrors()) {

                userService.save(userVo.toUser());
                return new Result("添加用户成功", 1);
            } else {
                return new Result("添加用户失败", 0);
            }
        } catch (Exception e) {
            LOG.error("添加用户失败", e);
            return new Result("添加用户失败", 0);
        }
    }




    @ApiOperation(value = "删除用户")
    @RequestMapping(value = "delete/{ids}", method = RequestMethod.DELETE)
    private Result delete(@PathVariable String ids){
        String[] arr=ids.split(",");
        if (null != arr) {
            for(int i=0;i<arr.length;i++){
                userService.deleteById(Integer.parseInt(arr[i]));
            }
            return new Result("success", 1);
        } else {
            return new Result("fail", 0);
        }
    }

    @ApiOperation(value = "修改用户")
    @RequestMapping(method = RequestMethod.PUT)
    private Result update(@Valid @RequestBody UserVo userVo, final BindingResult result) {
        try {
            if (!result.hasErrors()) {
                userService.save(userVo.toUser());
                return new Result("修改用户成功", 1);
            } else {
                return new Result("修改用户失败", 0);
            }
        } catch (Exception e) {
            LOG.error("修改用户失败", e);
            return new Result("修改用户失败", 0);
        }
    }
    @ApiOperation(value = "登录")
    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "用户名", required = true, paramType = "form"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "form")})
    public Result login( String name,  String password) {
        User user=userService.login(name,password);
        session.setAttribute("uid",user.getId());
        return new Result("登录成功",1);
    }

}
